package org.qdao.model;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.qdao.TableDescriptor;
import org.qdao.annotation.Column;
import org.qdao.annotation.Column.DBColumnType;
import org.qdao.annotation.PrimaryKey;
import org.qdao.annotation.Table;

/**
 * 
 * @author 譚元吉
 * @since 2010/01/26 15:58:48
 */
@Table(name = "T_PROGRAM", description = "我的程序")
public class Program {
	@Column(name = "PRO_ID", description = "程序ID", decimal = 1, length = 64, type = DBColumnType.VARCHAR, nullable = false)
	@PrimaryKey()
	public String identical;

	@Column(name = "PRO_DES", description = "程序注释", decimal = 2, length = 50, type = DBColumnType.VARCHAR, nullable = false)
	public String description;

	public static void main(String args[]) {

		StringBuffer buf = getSQLTigger();
		System.out.println(buf);
	}

	private static StringBuffer getSQLTigger() {
		TableDescriptor table = getTableDescriptor(Program.class);
		StringBuffer buf = new StringBuffer("CREATE TABLE ");
		buf.append(table.name + "(\r\n");
		for (int i = 0; i < table.columns.length; i++) {
			buf.append("\t" + table.columns[i].name + ' ')
					.append(table.columns[i].type)
					.append("(" + table.columns[i].length + ") ");
			if (!table.columns[i].nullable) {
				buf.append("NOT NULL");
				if (i == table.columns.length - 1 && table.primaryKeys.length == 0) {
					buf.append("\r\n");
				} else {
					buf.append(",\r\n");
				}
			}
		}
		if (table.primaryKeys.length != 0) {
			buf.append("\tCONSTRAINT PK_").append(table.name).append(" PRIMARY KEY (");
		}
		for (int i = 0; i < table.primaryKeys.length; i++) {
			if (i != table.primaryKeys.length - 1) {
				buf.append(table.primaryKeys[i]).append(',');
			} else {
				buf.append(table.primaryKeys[i]).append(")\r\n");
			}
		}

		buf.append(')');
		if (table.description != null && !"".equals(table.description)) {
			buf.append("\r\n/\r\nCOMMENT ON TABLE ").append(table.name).append(" IS ").append("'").append(table.description).append("'\r\n/");
		} else {
			buf.append("\r\n");
		}
		for (int i = 0; i < table.columns.length; i++) {
			if (table.columns[i].description != null &&
					!"".equals(table.columns[i].description)) {
				buf.append("\r\nCOMMENT ON COLUMN  ").append(table.name).append(".").append(table.columns[i].name).append(" IS ").append("'").append(table.columns[i].description).append(
						"'\r\n/");
			}
		}
		return buf;
	}

	public static TableDescriptor getTableDescriptor(Class<?> tableClazz) {
		TableDescriptor tableDescriptor = new TableDescriptor();

		Table table = (Table) tableClazz.getAnnotation(Table.class);
		if (table != null) {
			tableDescriptor.name = table.name();
			tableDescriptor.description = table.description();
		}

		Field[] fields = tableClazz.getDeclaredFields();
		List<TableDescriptor.ColumnDescription> columnDescriptors =
				new ArrayList<TableDescriptor.ColumnDescription>();

		List<String> primaryKeys = new ArrayList<String>();

		for (int i = 0; i < fields.length; i++) {
			Column column = fields[i].getAnnotation(Column.class);
			if (column != null) {
				TableDescriptor.ColumnDescription columnDescription = new TableDescriptor.ColumnDescription();
				columnDescription.decimal = column.decimal();
				columnDescription.description = column.description();
				columnDescription.name = column.name();
				columnDescription.length = column.length();
				columnDescription.nullable = column.nullable();
				columnDescription.type = column.type();
				columnDescriptors.add(columnDescription);
			}
			PrimaryKey primaryKey = fields[i].getAnnotation(PrimaryKey.class);
			if (primaryKey != null) {
				primaryKeys.add(column.name());
			}
		}

		// set the columns
		tableDescriptor.columns =
				columnDescriptors.toArray(
				new TableDescriptor.ColumnDescription[columnDescriptors.size()]);
		tableDescriptor.primaryKeys = primaryKeys.toArray(new String[primaryKeys.size()]);
		return tableDescriptor;
	}
}
